package com.hdoit.apollo.logical.billing.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.druid.util.StringUtils;
import com.hdoit.apollo.logical.billing.dao.IBillingDao;
import com.hdoit.apollo.logical.billing.model.Billing;
import com.hdoit.apollo.logical.billing.model.SellingPrice;
import com.hdoit.apollo.logical.billing.service.IBillingRecordService;
import com.hdoit.apollo.logical.billing.service.IBillingService;
import com.hdoit.apollo.logical.billing.service.ISellingPriceService;
import com.hdoit.apollo.logical.cargo.model.Collocation;
import com.hdoit.apollo.logical.cargo.service.ICollocationService;
import com.hdoit.apollo.logical.common.enums.billing.BillingRecordType;
import com.hdoit.apollo.logical.common.enums.billing.BillingState;
import com.hdoit.apollo.logical.common.enums.billing.BillingType;
import com.hdoit.apollo.logical.common.enums.billing.PayState;
import com.hdoit.apollo.logical.common.enums.outStock.OutStockState;
import com.hdoit.apollo.logical.common.enums.outStock.OutStockType;
import com.hdoit.apollo.logical.common.enums.shop.CustomerRecordType;
import com.hdoit.apollo.logical.common.enums.shop.PaymentState;
import com.hdoit.apollo.logical.shop.model.Customer;
import com.hdoit.apollo.logical.shop.model.CustomerRecord;
import com.hdoit.apollo.logical.shop.model.Payment;
import com.hdoit.apollo.logical.shop.service.ICustomerRecordService;
import com.hdoit.apollo.logical.shop.service.ICustomerService;
import com.hdoit.apollo.logical.shop.service.IPaymentService;
import com.hdoit.apollo.logical.stock.model.OutStock;
import com.hdoit.apollo.logical.stock.model.OutStockCollocation;
import com.hdoit.apollo.logical.stock.model.OutStockShow;
import com.hdoit.apollo.logical.stock.model.Stock;
import com.hdoit.apollo.logical.stock.model.StockCollocation;
import com.hdoit.apollo.logical.stock.service.IOutStockCollocationService;
import com.hdoit.apollo.logical.stock.service.IOutStockService;
import com.hdoit.apollo.logical.stock.service.IOutStockShowService;
import com.hdoit.apollo.logical.stock.service.IStockCollocationService;
import com.hdoit.apollo.logical.stock.service.IStockService;
import com.hdoit.apollo.logical.viewmodel.billing.BillingSearch;
import com.hdoit.apollo.logical.viewmodel.billing.BillingStat;
import com.hdoit.apollo.logical.viewmodel.shop.PaymentSearch;
import com.hdoit.apollo.logical.viewmodel.stock.OutStockCollocationInfo;
import com.hdoit.apollo.logical.viewmodel.stock.OutStockSearch;
import com.hdoit.apollo.logical.viewmodel.stock.ReturnCargoInfo;
import com.tennetcn.core.common.enums.pub.YesOrNoInteger;
import com.tennetcn.core.common.utils.CommonUtils;
import com.tennetcn.core.common.utils.DateUtils;
import com.tennetcn.data.dao.base.impl.SuperService;
import com.tennetcn.data.enums.ModelStatus;
import com.tennetcn.data.message.PagerModel;

import cn.hutool.core.convert.Convert;
/**
 * @author      tancongzhu
 * @email       
 * @createtime  2018年04月02日 10:40:50
 * @comment
 */
@Component
public class BillingServiceImpl extends SuperService<Billing> implements IBillingService{
	
	@Autowired
	private IOutStockService outStockService;
	@Autowired
	private IOutStockShowService outStockShowService;
	@Autowired
	private IOutStockCollocationService outStockCollocationService;
	@Autowired
	private IBillingRecordService billingRecordService;
	@Autowired
	private IBillingDao billingDao;
	@Autowired
	private IStockService stockService;
	@Autowired
	private IStockCollocationService stockCollocationService;
	@Autowired
	private ICustomerRecordService customerRecordService;
	@Autowired
	private IPaymentService paymentService;
	@Autowired
	private ICustomerService customerService;
	@Autowired
	private ICollocationService collocationService;
	
	@Autowired
	private ISellingPriceService sellingPriceService;
	
	//ionic上开销售单和退货单的保存
	@Override
	public boolean applyBilling(Billing billing, List<OutStockShow> outStockShows) {
		OutStock outStock =new OutStock();
		
		List<OutStockCollocation> outStockCollocations =new ArrayList<OutStockCollocation>();
		if(super.applyChange(billing)){
			if(billing.getModelStatus() == ModelStatus.add){
				
				outStock.setOutStockId(CommonUtils.getKeyId());
				outStock.setOutStockNum(billing.getTotalNum());
				outStock.setOutStockTypeNum(billing.getTypeNum());
				outStock.setOutStockDebt(billing.getTotalNum());
				if(BillingState.notShipped.equals(billing.getBillingState())) {
					outStock.setType(OutStockType.fahuo);
				}
				if(BillingState.returnOfCargo.equals(billing.getBillingState())) {
					outStock.setType(OutStockType.retnrnOfCargo);
				}
				outStock.setCreateDate(DateUtils.getCurrent());
				outStock.setCreateUserId(billing.getCreateUserId());
				outStock.setCreateUserName(billing.getCreateUserName());
				outStock.setShopId(billing.getShopId());
				outStock.setReturnCargoNum(0.0);
				outStock.setOutStockState(OutStockState.daihuku);
				outStock.setBillingId(billing.getBillingId());
				outStock.setDeleteMark(YesOrNoInteger.no);
				outStock.setModelStatus(ModelStatus.add);
				
				
				for (OutStockShow outStockShow : outStockShows) {
					if (StringUtils.isEmpty(outStockShow.getShowId())) {
						outStockShow.setShowId(CommonUtils.getKeyId());
					}
					outStockShow.setOutStockId(outStock.getOutStockId());
					outStockShow.setOutStockShowDebt(outStockShow.getOutStockShowNum());
					outStockShow.setDeleteMark(YesOrNoInteger.no);
					outStockShow.setCreateDate(DateUtils.getCurrent());
					outStockShow.setCreateUserId(billing.getCreateUserId());
					outStockShow.setCreateUserName(billing.getCreateUserName());
					outStockShow.setModelStatus(ModelStatus.add);
					Stock stock=new Stock();
					if(BillingState.returnOfCargo.equals(billing.getBillingState())) {
						//退货增加库存
						stock=stockService.queryModelByKey(outStockShow.getCargoId());
					}

					List<StockCollocation> stockCollocations = new ArrayList<StockCollocation>();
					for (OutStockCollocation outStockCollocation : outStockShow.getOutStockCollocations()) {
						if (StringUtils.isEmpty(outStockCollocation.getOutStockCollocationId())) {
							outStockCollocation.setOutStockCollocationId(CommonUtils.getKeyId());
						}
						outStockCollocation.setCustomerId(billing.getCustomerId());
						outStockCollocation.setCollocationDebt(outStockCollocation.getCollocationNum());
						outStockCollocation.setDeleteMark(YesOrNoInteger.no);
						outStockCollocation.setModelStatus(ModelStatus.add);
						outStockCollocation.setCreateDate(DateUtils.getCurrent());
						outStockCollocation.setCreateUserId(billing.getCreateUserId());
						outStockCollocation.setCreateUserName(billing.getCreateUserName());
						outStockCollocations.add(outStockCollocation);
						if(BillingState.returnOfCargo.equals(billing.getBillingState())) {
							for(StockCollocation stockCollocation:stock.getStockCollocations()) {
								if(stockCollocation.getCollocationId().equals(outStockCollocation.getCollocationId())) {
									stockCollocation.setModelStatus(ModelStatus.update);
									if(outStockCollocation.getRatio()!=null&&outStockCollocation.getRatio()!=0) {
										stockCollocation.setCollocationNum(stockCollocation.getCollocationNum()+outStockCollocation.getCollocationNum()*outStockCollocation.getRatio());
										stock.setStockNum(stock.getStockNum() + outStockCollocation.getCollocationNum()*outStockCollocation.getRatio());
									}else {
										stockCollocation.setCollocationNum(stockCollocation.getCollocationNum()+outStockCollocation.getCollocationNum());
										stock.setStockNum(stock.getStockNum() + outStockCollocation.getCollocationNum());
									}
									stockCollocations.add(stockCollocation);
								}
							}
						}
					}
					if(BillingState.returnOfCargo.equals(billing.getBillingState())) {
						//退货增加库存
						if( stock!= null){
							stock.setModelStatus(ModelStatus.update);
							stockService.applyChange(stock);
						}
					}
					if(BillingState.returnOfCargo.equals(billing.getBillingState())) {
						stockCollocationService.applyChanges(stockCollocations);
					}
				}

			}else{
				for (OutStockShow outStockShow : outStockShows) {
					outStockShow.setModelStatus(ModelStatus.update);
					for (OutStockCollocation outStockCollocation : outStockShow.getOutStockCollocations()) {
						outStockCollocation.setModelStatus(ModelStatus.update);
						outStockCollocations.add(outStockCollocation);
					}
				}

			}
			
			billingRecordService.saveBillingRecord(billing.getBillingId(),billing.getTotalNum(),billing.getBillingMoney(),BillingRecordType.openBill,
					"",billing.getShopId(),billing.getCreateUserId(),billing.getCreateUserName());
			
			if(BillingState.notShipped.equals(billing.getBillingState())) {
				customerRecordService.saveCustomerRecord(billing.getCustomerId(),billing.getBillingMoney(),0.0,CustomerRecordType.openBill,
						billing.getBillingId(),billing.getShopId(),billing.getCreateUserId(),billing.getCreateUserName());
			}
			if(BillingState.returnOfCargo.equals(billing.getBillingState())) {
				customerRecordService.saveCustomerRecord(billing.getCustomerId(),billing.getBillingMoney(),0.0,CustomerRecordType.returnOfCargo,
						billing.getBillingId(),billing.getShopId(),billing.getCreateUserId(),billing.getCreateUserName());
			}

			
			outStockService.applyChange(outStock);
			return outStockShowService.applyChanges(outStockShows)&&outStockCollocationService.applyChanges(outStockCollocations);
		}
		return false;
	}
	
	@Override
	public List<Billing> getBillingInfoByCargoId(String cargoId) {
		
		return billingDao.getBillingInfoByCargoId(cargoId);
	}

	@Override
	public boolean applyBillingReturn(Billing billing, OutStock outStock) {
		List<OutStockCollocation> outStockCollocations =new ArrayList<OutStockCollocation>();
		List<OutStockShow> outStockShows =new ArrayList<OutStockShow>();
		List<OutStockCollocation> outStockCollocationsYuan =new ArrayList<OutStockCollocation>();
		List<OutStockShow> outStockShowsYuan =new ArrayList<OutStockShow>();
		Billing billingYuan=billingDao.getBillingByKey(billing.getBillingIdReturn());
		if(super.applyChange(billing)){
			if(billing.getModelStatus() == ModelStatus.add){
				
				outStock.setType(OutStockType.returnCargo);
				outStock.setOutStockState(OutStockState.daihuku);
				outStock.setBillingId(billing.getBillingId());
				outStock.setDeleteMark(YesOrNoInteger.no);
				outStock.setShopId(billing.getShopId());
				outStock.setCreateDate(DateUtils.getCurrent());
				outStock.setCreateUserId(billing.getCreateUserId());
				outStock.setCreateUserName(billing.getCreateUserName());
				outStock.setModelStatus(ModelStatus.add);
				
				billingYuan.getOutStock().setOutStockDebt(billingYuan.getOutStock().getOutStockDebt()-outStock.getReturnCargoNum());
				if(billingYuan.getOutStock().getOutStockDebt()<=0) {
					billingYuan.setBillingState(BillingState.completed);
					billingYuan.setModelStatus(ModelStatus.update);
					super.applyChange(billingYuan);
				}
				if(StringUtils.isEmpty(String.valueOf(billingYuan.getOutStock().getReturnCargoNum()))) {
					billingYuan.getOutStock().setReturnCargoNum(outStock.getReturnCargoNum());
				}else {
					billingYuan.getOutStock().setReturnCargoNum(billingYuan.getOutStock().getReturnCargoNum()+outStock.getReturnCargoNum());
				}
				
				billingYuan.getOutStock().setModelStatus(ModelStatus.update);
				outStockService.applyChange(billingYuan.getOutStock());
				
				
				for (OutStockShow outStockShow : outStock.getOutStockShows() ) {
					outStockShow.setShowId(CommonUtils.getKeyId());
					outStockShow.setOutStockId(outStock.getOutStockId());
					outStockShow.setDeleteMark(YesOrNoInteger.no);
					outStockShow.setCreateDate(DateUtils.getCurrent());
					outStockShow.setCreateUserId(billing.getCreateUserId());
					outStockShow.setCreateUserName(billing.getCreateUserName());
					outStockShow.setModelStatus(ModelStatus.add);
					outStockShows.add(outStockShow);
					
					for (OutStockShow outStockShowYuan : billingYuan.getOutStock().getOutStockShows() ) {
						if(outStockShow.getCargoId().equals(outStockShowYuan.getCargoId())) {
							outStockShowYuan.setOutStockShowDebt(outStockShowYuan.getOutStockShowDebt()-outStockShow.getReturnCargoNumShow());
							outStockShowYuan.setModelStatus(ModelStatus.update);
							outStockShowsYuan.add(outStockShowYuan);
						}
					}
					for (OutStockCollocation outStockCollocation : outStockShow.getOutStockCollocations()) {
						outStockCollocation.setOutStockCollocationId(CommonUtils.getKeyId());
						outStockCollocation.setCustomerId(billing.getCustomerId());
						outStockCollocation.setShowId(outStockShow.getShowId());
						outStockCollocation.setDeleteMark(YesOrNoInteger.no);
						outStockCollocation.setCreateDate(DateUtils.getCurrent());
						outStockCollocation.setCreateUserId(billing.getCreateUserId());
						outStockCollocation.setCreateUserName(billing.getCreateUserName());
						outStockCollocation.setModelStatus(ModelStatus.add);
						
						outStockCollocations.add(outStockCollocation);
						for (OutStockShow outStockShowYuan : billingYuan.getOutStock().getOutStockShows() ) {
							for (OutStockCollocation outStockCollocationYuan : outStockShowYuan.getOutStockCollocations()) {
								if(outStockCollocation.getCollocationId().equals(outStockCollocationYuan.getCollocationId())) {
									outStockCollocationYuan.setCollocationDebt(outStockCollocationYuan.getCollocationDebt()-outStockCollocation.getReturnCargoNumCollocation());
									outStockCollocationYuan.setModelStatus(ModelStatus.update);
									outStockCollocationsYuan.add(outStockCollocationYuan);
								}
							}
						}
					}
					
				}

			}else{
				for (OutStockShow outStockShow : outStockShows) {
					outStockShow.setModelStatus(ModelStatus.update);
					for (OutStockCollocation outStockCollocation : outStockShow.getOutStockCollocations()) {
						outStockCollocation.setModelStatus(ModelStatus.update);
						outStockCollocations.add(outStockCollocation);
					}
				}

			}
			
			billingRecordService.saveBillingRecord(billing.getBillingIdReturn(),outStock.getReturnCargoNum(),0.0,BillingRecordType.returnCargo,
					billing.getBillingId(),billing.getShopId(),billing.getCreateUserId(),billing.getCreateUserName());
			
			billingRecordService.saveBillingRecord(billing.getBillingId(),outStock.getReturnCargoNum(),billing.getBillingMoney(),BillingRecordType.openBill,
					"",billing.getShopId(),billing.getCreateUserId(),billing.getCreateUserName());
			
			customerRecordService.saveCustomerRecord(billing.getCustomerId(),billing.getBillingMoney(),0.0,CustomerRecordType.returnCargo,
					billing.getBillingId(),billing.getShopId(),billing.getCreateUserId(),billing.getCreateUserName());
			
			outStockShowService.applyChanges(outStockShowsYuan);
			outStockCollocationService.applyChanges(outStockCollocationsYuan);
			outStockService.applyChange(outStock);
			return outStockShowService.applyChanges(outStockShows)&&outStockCollocationService.applyChanges(outStockCollocations);
		}
		return false;
	}
	
	@Override
	public Billing getBillingByKey(String key) {
		return billingDao.getBillingByKey(key);
	}
	
	@Override
	public List<Billing> getBillingInfoListByCustomerId(String customerId) {
		return billingDao.getBillingInfoListByCustomerId(customerId);
	}

	@Override
	public BillingStat getBillingStat(BillingSearch search) {
		return billingDao.getBillingStat(search);
	}

	@Override
	public boolean applyBillingAndOutStock(Billing billing, OutStockCollocationInfo outStockCollocationInfo) {
		Billing billingYuan=null;
		if(ModelStatus.add.equals(billing.getModelStatus())) {
			return saveBillingAndOutStock(billing,billingYuan,outStockCollocationInfo);
		}else {
			billingYuan =billingDao.getBillingByKey(billing.getBillingId());
			for(OutStockShow outStockShow:billingYuan.getOutStock().getOutStockShows()) {
				outStockCollocationService.deleteOutStockShowCollocation(outStockShow.getShowId());
			}
			outStockShowService.deleteOutStockShow(billingYuan.getOutStock().getOutStockId());
			if(outStockService.deleteOsutStockByKey(billingYuan.getOutStock().getOutStockId())) {
				return saveBillingAndOutStock(billing,billingYuan,outStockCollocationInfo);
			}
		}
		return true;
	}
	
	private boolean saveBillingAndOutStock(Billing billing,Billing billingYuan, OutStockCollocationInfo outStockCollocationInfo){
		if(StringUtils.isEmpty(outStockCollocationInfo.getCargoId())){
			return true;
		}else {
			String[] cargoIds=outStockCollocationInfo.getCargoId().split(",");
			String[] collocationIds=outStockCollocationInfo.getCollocationId().split(",");
			String[] collocationNums=outStockCollocationInfo.getCollocationNum().split(",");
			String[] realPrices=outStockCollocationInfo.getRealPrice().split(",");
			String[] units=outStockCollocationInfo.getUnit().split(",");
			/*String[] comments=outStockCollocationInfo.getComments().split(",");*/
			if (cheakIsRepeat(collocationIds)) {
				sellingPriceService.deleteSellingPrice(billing.getCustomerId(), collocationIds);
			}
			OutStock outStock=new OutStock();
			outStock.setOutStockId(CommonUtils.getKeyId());
			if(BillingState.notShipped.equals(billing.getBillingState())) {
				outStock.setType(OutStockType.fahuo);
			}
			if(BillingState.returnOfCargo.equals(billing.getBillingState())) {
				outStock.setType(OutStockType.retnrnOfCargo);
			}
			outStock.setCreateDate(billing.getCreateDate());
			outStock.setCreateUserId(billing.getCreateUserId());
			outStock.setCreateUserName(billing.getCreateUserName());
			
			outStock.setModifyDate(billing.getModifyDate());
			outStock.setModifyUserId(billing.getModifyUserId());
			outStock.setModifyUserName(billing.getModifyUserName());
            
			outStock.setShopId(billing.getShopId());
			outStock.setOutStockState(OutStockState.daihuku);
			outStock.setBillingId(billing.getBillingId());
			outStock.setDeleteMark(YesOrNoInteger.no);
			outStock.setModelStatus(ModelStatus.add);
			
			List<OutStockShow> outStockShows=new ArrayList<OutStockShow>();
			
			List<OutStockCollocation> outStockCollocations=new ArrayList<OutStockCollocation>();
			List<SellingPrice> sellingPrices=new ArrayList<SellingPrice>();
			SellingPrice sellingPrice=null;
			for(int i=0;i<collocationIds.length;i++){
				if(!StringUtils.isEmpty(collocationIds[i])) {
					//查询cargo单位比率
					Collocation collocation=collocationService.queryModelByCargo(collocationIds[i]);
					
					OutStockCollocation outStockCollocation=new OutStockCollocation();
					Double collocationNum=Double.parseDouble(collocationNums[i]);
					Double realPrice=Double.parseDouble(realPrices[i]);
					Boolean isCreateOutStockShow=true;
					sellingPrice=new SellingPrice();
					if(isCreateOutStockShow) {
						OutStockShow outStockShow=new OutStockShow();
						outStockShow.setShowId(CommonUtils.getKeyId());
						outStockShow.setOutStockId(outStock.getOutStockId());
						//outStockShow.setShowPrice(collocationNum*realPrice);
						//outStockShow.setOutStockShowNum(collocationNum);
						//单位换算
						if (units[i].equals(collocation.getCargo().getUnit())) {
							outStockShow.setShowPrice(collocationNum*realPrice);
							sellingPrice.setSellingPrice(realPrice);
							outStockShow.setOutStockShowNum(collocationNum);
						}else if (units[i].equals(collocation.getCargo().getViceFirstUnit())) {
							outStockShow.setShowPrice(collocationNum*realPrice*collocation.getCargo().getFirstRatio());
							sellingPrice.setSellingPrice(realPrice*collocation.getCargo().getFirstRatio());
							outStockShow.setOutStockShowNum(collocationNum*collocation.getCargo().getFirstRatio());
						}else if (units[i].equals(collocation.getCargo().getViceSecondUnit())) {
							outStockShow.setShowPrice(collocationNum*realPrice*collocation.getCargo().getSecondRatio());
							sellingPrice.setSellingPrice(realPrice*collocation.getCargo().getSecondRatio());
							outStockShow.setOutStockShowNum(collocationNum*collocation.getCargo().getSecondRatio());
						}
						//
						outStockShow.setOutStockShowDebt(outStockShow.getOutStockShowNum());
						outStockShow.setCargoId(cargoIds[i]);
						outStockShow.setReturnCargoNumShow(0.0);
						outStockShow.setModelStatus(ModelStatus.add);
						outStockShow.setDeleteMark(YesOrNoInteger.no);
						outStockShow.setCreateDate(billing.getCreateDate());
						outStockShow.setCreateUserId(billing.getCreateUserId());
						outStockShow.setCreateUserName(billing.getCreateUserName());
						outStockShows.add(outStockShow);
						outStockCollocation.setShowId(outStockShow.getShowId());
					}
					outStockCollocation.setOutStockCollocationId(CommonUtils.getKeyId());
					outStockCollocation.setCustomerId(billing.getCustomerId());
					outStockCollocation.setCollocationId(collocationIds[i]);
					outStockCollocation.setCollocationNum(collocationNum);
					outStockCollocation.setCollocationDebt(collocationNum);
					outStockCollocation.setCreateDate(billing.getCreateDate());
					outStockCollocation.setCreateUserId(billing.getCreateUserId());
					outStockCollocation.setCreateUserName(billing.getCreateUserName());
					outStockCollocation.setSendCargoNum(0.0);
					outStockCollocation.setReturnCargoNumCollocation(0.0);
					outStockCollocation.setShopId(billing.getShopId());
					outStockCollocation.setRealPrice(realPrice);
					outStockCollocation.setDeleteMark(YesOrNoInteger.no);
					outStockCollocation.setUnit(units[i]);
					
					
					//存入单位换算比率，方便库存操作
					if (units[i].equals(collocation.getCargo().getUnit())) {
						outStockCollocation.setRatio(1.0);
					}else if (units[i].equals(collocation.getCargo().getViceFirstUnit())) {
						outStockCollocation.setRatio(collocation.getCargo().getFirstRatio());
					}else if (units[i].equals(collocation.getCargo().getViceSecondUnit())) {
						outStockCollocation.setRatio(collocation.getCargo().getSecondRatio());
					}
					//
					if (cheakIsRepeat(collocationIds)) {
						sellingPrice.setId(CommonUtils.getKeyId());
						sellingPrice.setCollocationId(collocationIds[i]);
						sellingPrice.setCustomerId(billing.getCustomerId());
						sellingPrice.setModelStatus(ModelStatus.add);
						sellingPrice.setDeleteMark(YesOrNoInteger.no);
						sellingPrice.setCreateDate(billing.getCreateDate());
						sellingPrice.setCreateUserId(billing.getCreateUserId());
						sellingPrice.setCreateUserName(billing.getCreateUserName());
						sellingPrices.add(sellingPrice);
					}
					/*outStockCollocation.setComment(comments[i]);*/
					outStockCollocation.setModelStatus(ModelStatus.add);
					outStockCollocations.add(outStockCollocation);
				}
			}
			Double outStockNumAll=0.0;
			Double billingMoneyAll=0.0;
			List<StockCollocation> stockCollocations = new ArrayList<StockCollocation>();
			for(OutStockShow outStockShow:outStockShows) {
				outStockNumAll+=outStockShow.getOutStockShowNum();
				billingMoneyAll+=outStockShow.getShowPrice();
				if(BillingState.returnOfCargo.equals(billing.getBillingState())) {
					Stock stock=stockService.queryModelByKey(outStockShow.getCargoId());
					if( stock!= null){
						stock.setModelStatus(ModelStatus.update);
						stock.setStockNum(stock.getStockNum() + outStockShow.getOutStockShowNum());
						stockService.applyChange(stock);
					}
					for(StockCollocation stockCollocation:stock.getStockCollocations()) {
						for (OutStockCollocation outStockCollocation : outStockCollocations) {
							if(stockCollocation.getCollocationId().equals(outStockCollocation.getCollocationId())) {
								stockCollocation.setModelStatus(ModelStatus.update);
								stockCollocation.setCollocationNum(stockCollocation.getCollocationNum()+outStockCollocation.getCollocationNum()*outStockCollocation.getRatio());
								stockCollocations.add(stockCollocation);
							}
						}
			    	}
				}
			}
			if (cheakIsRepeat(collocationIds)) {
				sellingPriceService.applyChanges(sellingPrices);
			}
			stockCollocationService.applyChanges(stockCollocations);
			
			outStock.setOutStockNum(outStockNumAll);
			outStock.setOutStockDebt(outStockNumAll);
			outStock.setOutStockTypeNum(outStockShows.size());
			outStock.setReturnCargoNum(0.0);
			
			billing.setTypeNum(outStockShows.size());
			billing.setTotalNum(outStockNumAll);
			/*billing.setBillingMoney(billingMoneyAll);*/
			
			if(ModelStatus.add.equals(billing.getModelStatus())) {
				billingRecordService.saveBillingRecord(billing.getBillingId(),billing.getTotalNum(),billing.getBillingMoney(),BillingRecordType.openBill,
						"",billing.getShopId(),billing.getCreateUserId(),billing.getCreateUserName());

				if(BillingState.notShipped.equals(billing.getBillingState())) {
					customerRecordService.saveCustomerRecord(billing.getCustomerId(),billing.getBillingMoney(),0.0,CustomerRecordType.openBill,
							billing.getBillingId(),billing.getShopId(),billing.getCreateUserId(),billing.getCreateUserName());
				}
				if(BillingState.returnOfCargo.equals(billing.getBillingState())) {
					customerRecordService.saveCustomerRecord(billing.getCustomerId(),billing.getBillingMoney(),0.0,CustomerRecordType.returnOfCargo,
							billing.getBillingId(),billing.getShopId(),billing.getCreateUserId(),billing.getCreateUserName());
				}
			}else {
				
				billingRecordService.saveBillingRecord(billing.getBillingId(),billing.getTotalNum(),billing.getBillingMoney(),BillingRecordType.modify,
						"",billing.getShopId(),billing.getCreateUserId(),billing.getCreateUserName());
				billingYuan=super.queryModel(billing.getBillingId());

				if(BillingState.notShipped.equals(billing.getBillingState())) {
					customerRecordService.saveCustomerRecord(billing.getCustomerId(),billing.getBillingMoney()-billingYuan.getBillingMoney(),0.0,CustomerRecordType.modify,
							billing.getBillingId(),billing.getShopId(),billing.getCreateUserId(),billing.getCreateUserName());
				}
				if(BillingState.returnOfCargo.equals(billing.getBillingState())) {
					customerRecordService.saveCustomerRecord(billing.getCustomerId(),billing.getBillingMoney()-billingYuan.getBillingMoney(),0.0,CustomerRecordType.returnOfCargoModify,
							billing.getBillingId(),billing.getShopId(),billing.getCreateUserId(),billing.getCreateUserName());
				}
			}
			
			if(super.applyChange(billing)) {
				return outStockService.applyChange(outStock)&&outStockShowService.applyChanges(outStockShows)&&outStockCollocationService.applyChanges(outStockCollocations);
			}
			
		}
		return false;
	}
	
	private boolean cheakIsRepeat(String[] array) {
        HashSet<String> hashSet = new HashSet<String>();
        for (int i = 0; i < array.length; i++) {
            hashSet.add(array[i]);
        }
        if (hashSet.size() == array.length) {
            return true;
        } else {
            return false;
        }
    }

	@Override
	public boolean applyBillingAndReturnCargo(Billing billing, ReturnCargoInfo returnCargoInfo) {
		List<OutStockCollocation> outStockCollocationsYuan =new ArrayList<OutStockCollocation>();
		List<OutStockShow> outStockShowsYuan =new ArrayList<OutStockShow>();
		if(StringUtils.isEmpty(returnCargoInfo.getReturnCargoNumCollocation())){
			return true;
		}else {
			String[] cargoIds=returnCargoInfo.getCargoId().split(",");
			String[] collocationIds=returnCargoInfo.getCollocationId().split(",");
			String[] realPrices=returnCargoInfo.getRealPrice().split(",");
			String[] collocationDebts=returnCargoInfo.getCollocationDebt().split(",");
			String[] returnCargoNumCollocations=returnCargoInfo.getReturnCargoNumCollocation().split(",");
			
			Billing billingYuan=billingDao.getBillingByKey(billing.getBillingIdReturn());
			
			OutStock outStock=new OutStock();
			outStock.setOutStockId(CommonUtils.getKeyId());
			outStock.setType(OutStockType.returnCargo);
			outStock.setCreateDate(billing.getCreateDate());
			outStock.setCreateUserId(billing.getCreateUserId());
			outStock.setCreateUserName(billing.getCreateUserName());
			
			outStock.setModifyDate(billing.getModifyDate());
			outStock.setModifyUserId(billing.getModifyUserId());
			outStock.setModifyUserName(billing.getModifyUserName());
            
			outStock.setShopId(billing.getShopId());
			outStock.setOutStockState(OutStockState.daihuku);
			outStock.setBillingId(billing.getBillingId());
			outStock.setDeleteMark(YesOrNoInteger.no);
			outStock.setModelStatus(ModelStatus.add);
			
			List<OutStockShow> outStockShows=new ArrayList<OutStockShow>();
			
			List<OutStockCollocation> outStockCollocations=new ArrayList<OutStockCollocation>();
			for(int i=0;i<returnCargoNumCollocations.length;i++){
				if(Integer.parseInt(returnCargoNumCollocations[i])>0) {
					OutStockCollocation outStockCollocation=new OutStockCollocation();
					
					Double realPrice=Double.parseDouble(realPrices[i]);
					Double collocationDebt=Double.parseDouble(collocationDebts[i]);
					Double returnCargoNumCollocation=Double.parseDouble(returnCargoNumCollocations[i]);
					
					Boolean isCreateOutStockShow=true;
					for(OutStockShow outStockShow:outStockShows) {
						if(outStockShow.getCargoId().equals(cargoIds[i])) {
							outStockShow.setShowPrice(outStockShow.getShowPrice()+returnCargoNumCollocation*realPrice);
							outStockShow.setOutStockShowNum(0.0);
							outStockShow.setOutStockShowDebt(outStockShow.getOutStockShowDebt()+collocationDebt);
							outStockShow.setReturnCargoNumShow(outStockShow.getReturnCargoNumShow()+returnCargoNumCollocation);
							isCreateOutStockShow=false;
							outStockCollocation.setShowId(outStockShow.getShowId());
						}
					}
					if(isCreateOutStockShow) {
						OutStockShow outStockShow=new OutStockShow();
						outStockShow.setShowId(CommonUtils.getKeyId());
						outStockShow.setOutStockId(outStock.getOutStockId());
						outStockShow.setShowPrice(returnCargoNumCollocation*realPrice);
						outStockShow.setOutStockShowNum(0.0);
						outStockShow.setOutStockShowDebt(collocationDebt);
						outStockShow.setReturnCargoNumShow(returnCargoNumCollocation);
						outStockShow.setCreateDate(DateUtils.getCurrent());
						outStockShow.setCreateUserId(billing.getCreateUserId());
						outStockShow.setCreateUserName(billing.getCreateUserName());
						outStockShow.setDeleteMark(YesOrNoInteger.no);
						outStockShow.setCargoId(cargoIds[i]);
						outStockShow.setModelStatus(ModelStatus.add);
						outStockShows.add(outStockShow);
						outStockCollocation.setShowId(outStockShow.getShowId());
					}
					outStockCollocation.setOutStockCollocationId(CommonUtils.getKeyId());
					outStockCollocation.setCollocationId(collocationIds[i]);
					outStockCollocation.setCollocationNum(0.0);
					outStockCollocation.setCollocationDebt(collocationDebt);
					outStockCollocation.setSendCargoNum(0.0);
					outStockCollocation.setReturnCargoNumCollocation(returnCargoNumCollocation);
					outStockCollocation.setRealPrice(realPrice);
					outStockCollocation.setDeleteMark(YesOrNoInteger.no);
					/*outStockCollocation.setComment(comments[i]);*/
					outStockCollocation.setCreateDate(DateUtils.getCurrent());
					outStockCollocation.setCreateUserId(billing.getCreateUserId());
					outStockCollocation.setCreateUserName(billing.getCreateUserName());
					outStockCollocation.setModelStatus(ModelStatus.add);
					outStockCollocations.add(outStockCollocation);
					
					for (OutStockShow outStockShowYuan : billingYuan.getOutStock().getOutStockShows() ) {
						for (OutStockCollocation outStockCollocationYuan : outStockShowYuan.getOutStockCollocations()) {
							if(outStockCollocation.getCollocationId().equals(outStockCollocationYuan.getCollocationId())) {
								outStockCollocationYuan.setCollocationDebt(outStockCollocationYuan.getCollocationDebt()-outStockCollocation.getReturnCargoNumCollocation());
								outStockCollocationYuan.setModelStatus(ModelStatus.update);
								outStockCollocationsYuan.add(outStockCollocationYuan);
							}
						}
					}
				}
				
			}
			Double returnCargoNumAll=0.0;
			for(OutStockShow outStockShow:outStockShows) {
				returnCargoNumAll+=outStockShow.getReturnCargoNumShow();
				for (OutStockShow outStockShowYuan : billingYuan.getOutStock().getOutStockShows() ) {
					if(outStockShow.getCargoId().equals(outStockShowYuan.getCargoId())) {
						outStockShowYuan.setOutStockShowDebt(outStockShowYuan.getOutStockShowDebt()-outStockShow.getReturnCargoNumShow());
						outStockShowYuan.setModelStatus(ModelStatus.update);
						outStockShowsYuan.add(outStockShowYuan);
					}
				}
				
			}
			
			outStock.setOutStockNum(0.0);
			outStock.setOutStockDebt(0.0);
			outStock.setOutStockTypeNum(outStockShows.size());
			outStock.setReturnCargoNum(returnCargoNumAll);
			
			billing.setTypeNum(0);
			billing.setTotalNum(0.0);
			billing.setBillingState(BillingState.returnCargo);
			
			billingYuan.getOutStock().setOutStockDebt(billingYuan.getOutStock().getOutStockDebt()-outStock.getReturnCargoNum());
			if(billingYuan.getOutStock().getOutStockDebt()<=0) {
				billingYuan.setBillingState(BillingState.completed);
				billingYuan.setModelStatus(ModelStatus.update);
				super.applyChange(billingYuan);
			}
			if(StringUtils.isEmpty(String.valueOf(billingYuan.getOutStock().getReturnCargoNum()))) {
				billingYuan.getOutStock().setReturnCargoNum(outStock.getReturnCargoNum());
			}else {
				billingYuan.getOutStock().setReturnCargoNum(billingYuan.getOutStock().getReturnCargoNum()+outStock.getReturnCargoNum());
			}
			
			billingYuan.getOutStock().setModelStatus(ModelStatus.update);
			outStockService.applyChange(billingYuan.getOutStock());
			
			billingRecordService.saveBillingRecord(billing.getBillingIdReturn(),outStock.getReturnCargoNum(),0.0,BillingRecordType.returnCargo,
					billing.getBillingId(),billing.getShopId(),billing.getCreateUserId(),billing.getCreateUserName());
			
			billingRecordService.saveBillingRecord(billing.getBillingId(),outStock.getReturnCargoNum(),billing.getBillingMoney(),BillingRecordType.openBill,
					"",billing.getShopId(),billing.getCreateUserId(),billing.getCreateUserName());
			
			customerRecordService.saveCustomerRecord(billing.getCustomerId(),billing.getBillingMoney(),0.0,CustomerRecordType.returnCargo,
					billing.getBillingId(),billing.getShopId(),billing.getCreateUserId(),billing.getCreateUserName());
			
			outStockShowService.applyChanges(outStockShowsYuan);
			outStockCollocationService.applyChanges(outStockCollocationsYuan);
			if(super.applyChange(billing)) {
				return outStockService.applyChange(outStock)&&outStockShowService.applyChanges(outStockShows)&&outStockCollocationService.applyChanges(outStockCollocations);
			}
		}
		return false;
	}

	@Override
	public boolean closeBilling(Billing billing, String loginId, String loginName) {
		OutStockSearch search=new OutStockSearch();
		search.setBillingId(billing.getBillingId());
		List<OutStock> outStocks=outStockService.queryListBySearch(search);
		
		BillingSearch billingSearch=new BillingSearch();
		billingSearch.setBillingIdReturn(billing.getBillingId());
		List<Billing> billings=billingDao.queryListMP(billingSearch, null);
		
		PaymentSearch paymentSearch=new PaymentSearch();
		paymentSearch.setBillingId(billing.getBillingId());
		List<Payment> payments= paymentService.queryListMP(paymentSearch,null);
		
		if(payments != null && payments.size() !=0) {
			for(Payment payment:payments) {
				payment.setModelStatus(ModelStatus.update);
				payment.setBillingType(BillingType.close);
				Customer customer=customerService.queryModel(payment.getCustomerId());
				customer.setModelStatus(ModelStatus.update);
				if(PaymentState.pay.equals(payment.getPaymentState())) {
					customerRecordService.saveCustomerRecord(payment.getCustomerId(),0.0,payment.getAmount(),CustomerRecordType.pay,
							payment.getPaymentId(),payment.getShopId(),payment.getCreateUserId(),payment.getCreateUserName());
					if(customer.getPaymentMoney()==null){
						customer.setPaymentMoney(0.0);
					}
					customer.setPaymentMoney(customer.getPaymentMoney()-payment.getAmount());
					customer.setCustomerDebt(customer.getCustomerDebt()+payment.getAmount());
				}else if(PaymentState.refund.equals(payment.getPaymentState())) {
					if(customer.getPaymentMoney()==null){
						customer.setPaymentMoney(0.0);
					}
					
					customer.setPaymentMoney(customer.getPaymentMoney()+payment.getAmount());
					customer.setCustomerDebt(customer.getCustomerDebt()-payment.getAmount());
				}
				customerService.applyChange(customer);
			}
			paymentService.applyChanges(payments);
		}
		
		if(billings != null && billings.size() !=0) {
			for(Billing billingReturnCargo:billings) {
				billingReturnCargo.setBillingState(BillingState.close);
				billingReturnCargo.setModelStatus(ModelStatus.update);
		    	
		    	billingRecordService.saveBillingRecord(billingReturnCargo.getBillingId(),0.0,0.0,BillingRecordType.close,
						"",billing.getShopId(),loginId,loginName);
		    	
		    	customerRecordService.saveCustomerRecord(billingReturnCargo.getCustomerId(),-billingReturnCargo.getBillingMoney(),0.0,CustomerRecordType.close,
		    			billingReturnCargo.getBillingId(),billing.getShopId(),loginId,loginName);
			}
			super.applyChanges(billings);
		}
		if(outStocks != null && outStocks.size() !=0 ){
			for(OutStock outStock:outStocks) {
				outStock.setModelStatus(ModelStatus.update);
				outStock.setBillingType(BillingType.close);
				for (OutStockShow outStockShow : outStock.getOutStockShows()) {
					Stock stock=stockService.queryModelByKey(outStockShow.getCargoId());
					if( stock!= null){
						stock.setModelStatus(ModelStatus.update);
						
						stock.setStockNum(stock.getStockNum() + outStockShow.getOutStockShowNum());
						stockService.applyChange(stock);
					}
					
					for (OutStockCollocation outStockCollocation : outStockShow.getOutStockCollocations()) {
						
						for(StockCollocation stockCollocation:stock.getStockCollocations()) {
							if(stockCollocation.getCollocationId().equals(outStockCollocation.getCollocationId())) {
								stockCollocation.setModelStatus(ModelStatus.update);
								stockCollocation.setCollocationNum(stockCollocation.getCollocationNum()+outStockCollocation.getCollocationNum()*outStockCollocation.getRatio());
								stockCollocationService.applyChange(stockCollocation);
							}
						}
					}
					
				}
			}
			outStockService.applyChanges(outStocks);
			
		}
		return super.applyChange(billing);
	}

	@Override
	public boolean applyBillingReturnCheck(Billing billing, OutStockCollocationInfo outStockCollocationInfo) {
		boolean result=false;
		Billing billing2=getBillingByKey(billing.getBillingId());
		for (OutStockShow outStockShow : billing2.getOutStock().getOutStockShows()) {
			for (OutStockCollocation outStockCollocation : outStockShow.getOutStockCollocations()) {
				Double num=(outStockCollocation.getCollocationNum()-outStockCollocation.getCollocationDebt())*Convert.toDouble(outStockCollocation.getRatio(),1.0);
				if(num>0) {
					result=stockCollocationService.updateStockCollocationNo(outStockCollocation.getCollocationId(), num)&&stockCollocationService.updateStockNo(outStockCollocation.getCollocationId(), num);
				}else {
					result=true;
				}
			}
		}
		if(result) {
			result=outStockService.deleteOutStockCollocationData(billing.getBillingId())&&outStockService.deleteOutStockData(billing.getBillingId())&&outStockService.deleteOutStockShowData(billing.getBillingId());
			if(result) {
				result=saveBillingByReturnCheck(billing,billing2,outStockCollocationInfo);	
			}
		}
		return result;
	}
	
	private boolean saveBillingByReturnCheck(Billing billing,Billing billing2, OutStockCollocationInfo outStockCollocationInfo){
		if(StringUtils.isEmpty(outStockCollocationInfo.getCargoId())){
			return true;
		}else {
			String[] cargoIds=outStockCollocationInfo.getCargoId().split(",");
			String[] collocationIds=outStockCollocationInfo.getCollocationId().split(",");
			String[] collocationNums=outStockCollocationInfo.getCollocationNum().split(",");
			String[] realPrices=outStockCollocationInfo.getRealPrice().split(",");
			String[] units=outStockCollocationInfo.getUnit().split(",");
			
			if (cheakIsRepeat(collocationIds)) {
				sellingPriceService.deleteSellingPrice(billing.getCustomerId(), collocationIds);
			}
			OutStock outStock=new OutStock();
			outStock.setOutStockId(CommonUtils.getKeyId());
			if(BillingState.notShipped.equals(billing.getBillingState())) {
				outStock.setType(OutStockType.fahuo);
			}
			if(BillingState.returnOfCargo.equals(billing.getBillingState())) {
				outStock.setType(OutStockType.retnrnOfCargo);
			}
			outStock.setCreateDate(billing.getCreateDate());
			outStock.setCreateUserId(billing.getCreateUserId());
			outStock.setCreateUserName(billing.getCreateUserName());
			
			outStock.setModifyDate(billing.getModifyDate());
			outStock.setModifyUserId(billing.getModifyUserId());
			outStock.setModifyUserName(billing.getModifyUserName());
            
			outStock.setShopId(billing.getShopId());
			outStock.setOutStockState(OutStockState.daihuku);
			outStock.setBillingId(billing.getBillingId());
			outStock.setDeleteMark(YesOrNoInteger.no);
			outStock.setModelStatus(ModelStatus.add);
			
			List<OutStockShow> outStockShows=new ArrayList<OutStockShow>();
			
			List<OutStockCollocation> outStockCollocations=new ArrayList<OutStockCollocation>();
			List<SellingPrice> sellingPrices=new ArrayList<SellingPrice>();
			SellingPrice sellingPrice=null;
			for(int i=0;i<collocationIds.length;i++){
				if(!StringUtils.isEmpty(collocationIds[i])) {
					//查询cargo单位比率
					Collocation collocation=collocationService.queryModelByCargo(collocationIds[i]);
					
					OutStockCollocation outStockCollocation=new OutStockCollocation();
					Double collocationNum=Double.parseDouble(collocationNums[i]);
					Double realPrice=Double.parseDouble(realPrices[i]);
					Boolean isCreateOutStockShow=true;
					sellingPrice=new SellingPrice();
					if(isCreateOutStockShow) {
						OutStockShow outStockShow=new OutStockShow();
						outStockShow.setShowId(CommonUtils.getKeyId());
						outStockShow.setOutStockId(outStock.getOutStockId());
						//outStockShow.setShowPrice(collocationNum*realPrice);
						//outStockShow.setOutStockShowNum(collocationNum);
						//单位换算
						if (units[i].equals(collocation.getCargo().getUnit())) {
							outStockShow.setShowPrice(collocationNum*realPrice);
							sellingPrice.setSellingPrice(realPrice);
							outStockShow.setOutStockShowNum(collocationNum);
						}else if (units[i].equals(collocation.getCargo().getViceFirstUnit())) {
							outStockShow.setShowPrice(collocationNum*realPrice*collocation.getCargo().getFirstRatio());
							sellingPrice.setSellingPrice(realPrice*collocation.getCargo().getFirstRatio());
							outStockShow.setOutStockShowNum(collocationNum*collocation.getCargo().getFirstRatio());
						}else if (units[i].equals(collocation.getCargo().getViceSecondUnit())) {
							outStockShow.setShowPrice(collocationNum*realPrice*collocation.getCargo().getSecondRatio());
							sellingPrice.setSellingPrice(realPrice*collocation.getCargo().getSecondRatio());
							outStockShow.setOutStockShowNum(collocationNum*collocation.getCargo().getSecondRatio());
						}
						//
						outStockShow.setOutStockShowDebt(outStockShow.getOutStockShowNum());
						outStockShow.setCargoId(cargoIds[i]);
						outStockShow.setReturnCargoNumShow(0.0);
						outStockShow.setModelStatus(ModelStatus.add);
						outStockShow.setDeleteMark(YesOrNoInteger.no);
						outStockShow.setCreateDate(billing.getCreateDate());
						outStockShow.setCreateUserId(billing.getCreateUserId());
						outStockShow.setCreateUserName(billing.getCreateUserName());
						outStockShows.add(outStockShow);
						outStockCollocation.setShowId(outStockShow.getShowId());
					}
					outStockCollocation.setOutStockCollocationId(CommonUtils.getKeyId());
					outStockCollocation.setCustomerId(billing.getCustomerId());
					outStockCollocation.setCollocationId(collocationIds[i]);
					outStockCollocation.setCollocationNum(collocationNum);
					outStockCollocation.setCollocationDebt(collocationNum);
					outStockCollocation.setCreateDate(billing.getCreateDate());
					outStockCollocation.setCreateUserId(billing.getCreateUserId());
					outStockCollocation.setCreateUserName(billing.getCreateUserName());
					outStockCollocation.setSendCargoNum(0.0);
					outStockCollocation.setReturnCargoNumCollocation(0.0);
					outStockCollocation.setShopId(billing.getShopId());
					outStockCollocation.setRealPrice(realPrice);
					outStockCollocation.setDeleteMark(YesOrNoInteger.no);
					outStockCollocation.setUnit(units[i]);
					
					
					//存入单位换算比率，方便库存操作
					if (units[i].equals(collocation.getCargo().getUnit())) {
						outStockCollocation.setRatio(1.0);
					}else if (units[i].equals(collocation.getCargo().getViceFirstUnit())) {
						outStockCollocation.setRatio(collocation.getCargo().getFirstRatio());
					}else if (units[i].equals(collocation.getCargo().getViceSecondUnit())) {
						outStockCollocation.setRatio(collocation.getCargo().getSecondRatio());
					}
					//
					if (cheakIsRepeat(collocationIds)) {
						sellingPrice.setId(CommonUtils.getKeyId());
						sellingPrice.setCollocationId(collocationIds[i]);
						sellingPrice.setCustomerId(billing.getCustomerId());
						sellingPrice.setModelStatus(ModelStatus.add);
						sellingPrice.setDeleteMark(YesOrNoInteger.no);
						sellingPrice.setCreateDate(billing.getCreateDate());
						sellingPrice.setCreateUserId(billing.getCreateUserId());
						sellingPrice.setCreateUserName(billing.getCreateUserName());
						sellingPrices.add(sellingPrice);
					}
					/*outStockCollocation.setComment(comments[i]);*/
					outStockCollocation.setModelStatus(ModelStatus.add);
					outStockCollocations.add(outStockCollocation);
				}
			}
			Double outStockNumAll=0.0;
			Double billingMoneyAll=0.0;
			List<StockCollocation> stockCollocations = new ArrayList<StockCollocation>();
			for(OutStockShow outStockShow:outStockShows) {
				outStockNumAll+=outStockShow.getOutStockShowNum();
				billingMoneyAll+=outStockShow.getShowPrice();
				if(BillingState.returnOfCargo.equals(billing.getBillingState())) {
					Stock stock=stockService.queryModelByKey(outStockShow.getCargoId());
					if( stock!= null){
						stock.setModelStatus(ModelStatus.update);
						stock.setStockNum(stock.getStockNum() + outStockShow.getOutStockShowNum());
						stockService.applyChange(stock);
					}
					for(StockCollocation stockCollocation:stock.getStockCollocations()) {
						for (OutStockCollocation outStockCollocation : outStockCollocations) {
							if(stockCollocation.getCollocationId().equals(outStockCollocation.getCollocationId())) {
								stockCollocation.setModelStatus(ModelStatus.update);
								stockCollocation.setCollocationNum(stockCollocation.getCollocationNum()+outStockCollocation.getCollocationNum()*outStockCollocation.getRatio());
								stockCollocations.add(stockCollocation);
							}
						}
			    	}
				}
			}
			if (cheakIsRepeat(collocationIds)) {
				sellingPriceService.applyChanges(sellingPrices);
			}
			stockCollocationService.applyChanges(stockCollocations);
			
			outStock.setOutStockNum(outStockNumAll);
			outStock.setOutStockDebt(outStockNumAll);
			outStock.setOutStockTypeNum(outStockShows.size());
			outStock.setReturnCargoNum(0.0);
			billing.setTypeNum(outStockShows.size());
			billing.setTotalNum(outStockNumAll);
			Double relativePrice=billing2.getBillingMoney()-billing.getBillingMoney();
			if(Convert.toDouble(billing2.getPaidAmount(),0.0)==0.0) {
				billing.setPayState(PayState.unpaid);
				billing.setUnpaidAmount(billing.getBillingMoney());
			}else if(billing.getBillingMoney()-Convert.toDouble(billing2.getPaidAmount(),0.0)>0) {
				billing.setPayState(PayState.portion);
				billing.setUnpaidAmount(billing.getBillingMoney()-billing2.getPaidAmount());
				billing.setPaidAmount(billing2.getPaidAmount());
			}else if(billing.getBillingMoney()-Convert.toDouble(billing2.getPaidAmount(),0.0)<0) {
				billing.setPayState(PayState.outPaid);
				billing.setUnpaidAmount(billing.getBillingMoney()-billing2.getPaidAmount());
				billing.setPaidAmount(billing2.getPaidAmount());
			}else {
				billing.setPayState(PayState.paid);
				billing.setUnpaidAmount(0.0);
				billing.setPaidAmount(billing2.getPaidAmount());
			}
			
			if(super.applyChange(billing)) {
				
				Customer customer=customerService.queryModel(billing.getCustomerId());
				customer.setCustomerDebt(customer.getCustomerDebt()-relativePrice);
		
				customer.setModelStatus(ModelStatus.update);
				
				CustomerRecord customerRecord = new CustomerRecord();
				customerRecord.setModelStatus(ModelStatus.add);
				customerRecord.setRecordId(CommonUtils.getKeyId());
				customerRecord.setCustomerId(customer.getCustomerId());

				customerRecord.setReceivable(-relativePrice);
				customerRecord.setReceipt(0.0);

				
				customerRecord.setType(CustomerRecordType.returnCheck);
				customerRecord.setOperateId(billing.getBillingId());
				customerRecord.setShopId(billing.getShopId());
				customerRecord.setCreateDate(DateUtils.getCurrent());
				customerRecord.setCreateUserId(billing.getModifyUserId());
				customerRecord.setCreateUserName(billing.getCreateUserId());
				customerRecord.setDeleteMark(YesOrNoInteger.no);
				customerRecord.setAccumulativeDebt(customer.getCustomerDebt());
				billingRecordService.saveBillingRecord(billing.getBillingId(),billing.getTotalNum(),billing.getBillingMoney(),BillingRecordType.returnCheck,
						"",billing.getShopId(),billing.getCreateUserId(),billing.getCreateUserName());
				
			
				return customerService.applyChange(customer)&&customerRecordService.applyChange(customerRecord)&&outStockService.applyChange(outStock)&&outStockShowService.applyChanges(outStockShows)&&outStockCollocationService.applyChanges(outStockCollocations);
			}
			
		}
		return false;
	}

	@Override
	public List<Billing> queryListMPForWeChat(BillingSearch search,PagerModel pagerModel) {
		
		return billingDao.queryListMPForWeChat(search,pagerModel);
	}

}
